package com.main.messagenotificationmanagement.infrastructure.util;

import cn.dev33.satoken.stp.StpUtil;
import com.main.common.utils.SaTokenUtil;
import com.main.messagenotificationmanagement.interfaces.dto.MessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;

@Component
public class MessageRouter {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    public void route(MessageDTO message) {
        switch (message.getType()) {
            case SYSTEM:
                messagingTemplate.convertAndSend("/topic/system", message);
                break;
            case GROUP:
                messagingTemplate.convertAndSend("/topic/group" + message.getGroupId(), message);
                break;
            case CLIENT:
                messagingTemplate.convertAndSendToUser(message.getClientId(), "/queue/private", message);
                break;
            case USER:
                messagingTemplate.convertAndSendToUser(message.getUserId(), "/queue/private", message);
                break;
        }

    }
    private MessageHeaders createHeaders(String sessionId) {
        SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor
                .create(SimpMessageType.MESSAGE);
        headerAccessor.setSessionId(sessionId);
        headerAccessor.setLeaveMutable(true);
        return headerAccessor.getMessageHeaders();
    }
}